package ControlLayer;
import TUILayer.LoanUI;
import TUILayer.AddressBookUI;
import ModelLayer.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.text.DateFormat;
import java.util.Locale;

/**
 * Write a description of class LoanCtr here.
 * 
 * @author (Allan Lie) 
 * @version (V1)
 */
public class LoanCtr
{
    private LoanUI loanUI;
    private AddressBookUI adrUI;
    private AddressBook addressBookModel;
    private Person person;
    private DvdCollection dvdCollectionModel;
    private LoanCollection loanCollectionModel;
 
    public LoanCtr()
    {
        this.loanUI = new LoanUI();
        this.dvdCollectionModel = DvdCollection.getInstance();
        this.loanCollectionModel = LoanCollection.getInstance();
        this.addressBookModel = AddressBook.getInstance();
        
        Locale l = new Locale("da", "dk");
        l.setDefault(l);
        
        loanMenu();       
    }
    
    public void loanMenu() {
        boolean exit = false;
        
        while(!exit) {
            
            loanUI.writeLoanMenu();
            int choise = this.loanUI.readInt();
        
            if(choise == 1) {
                showLoans();
            } else if(choise == 2) {
                addLoan();
            } else if(choise == 3) {
                removeLoan();
            } else {
                exit = true;
            }
            
        }
    }
    
    public void showLoans() 
    {
        int id = loanSelect();
        Loan loans = loanCollectionModel.findLoan(id);        
     
        if(loans != null) 
        {
            loanUI.writeLoanDetails(loans);
            loanUI.writeEnterToContinue();
            loanUI.readLine();
        }    
    }
    
    public void addLoan()
    {
        int loanId = loanCollectionModel.findBiggestId()+1;
        
        int id = personSelect();
        
        Person person = addressBookModel.findPerson(id);
        if(person != null)
        {
            int idd = dvdSelect();
            Dvd dvd = dvdCollectionModel.findDvd(idd);
            
            if(dvd != null)
            {
                DvdCopy dvdCopy = dvd.findNotRentedCopy();
                if(dvdCopy != null)
                {
                    loanUI.writeEnterPeriod();
                    int period = loanUI.readInt();
                    if(period > 0) {
                        
                        DateFormat df = DateFormat.getDateInstance();
                        String date = df.format(new Date());

                        Loan loan = new Loan(loanId, date, period);
                        loan.setPerson(person);
                        loan.setDvd(dvd);
                        loan.setDvdCopy(dvdCopy);
                        loanCollectionModel.makeLoan(loan);
                    }
                }
            }
        }
    }
    
    public void removeLoan() {
        int id = this.loanSelect();
        
        if(id > 0) {
            Loan loan = loanCollectionModel.findLoan(id);
            if(loan != null) {
                loanUI.writeSureYouWantToLDelete();
                
                String choise = loanUI.readLine();
                if(choise.equals("y") || choise.equals("Y")) {
                    loan.getDvdCopy().setAsNotRented();
                    
                    if(this.returnedTooLate(loan.getDate(), loan.getPeriod())) {
                        loanUI.writeReturnedToLate();
                    }
                    
                    if(loanCollectionModel.removeLoan(id)) {
                        loanUI.writeLoanRemoved();
                        loanUI.writeEnterToContinue();
                        loanUI.readLine();
                    }
                }
            }
        }
    }
    
    private int personSelect()
    {
        loanUI.writeAddressbook();
        loanUI.writeOutPersons(addressBookModel.getAllPersons());
        
        loanUI.writeSelectId();
        
        int id = loanUI.readInt();
        return id;
    }
    
    private int dvdSelect()
    {
        loanUI.writeDvdList();
        loanUI.writeOutDvds(dvdCollectionModel.getAllDvds());
        loanUI.writeSelectId();
        
        int idd = loanUI.readInt();
        return idd;
        
    }
    
    public int loanSelect()
    {
        int id = 0;
        
        loanUI.writeLoanList();
        ArrayList<Loan> la = loanCollectionModel.getAllLoans();
        loanUI.writeOutLoans(la);
        
        if(la.size() > 0) {
            loanUI.writeSelectId();
            id = loanUI.readInt();
        } else {
            loanUI.writeEnterToContinue();
            loanUI.readLine();
        }
        
        return id;
    }
    
    public boolean returnedTooLate(String date, int period) {
        // Making sure that the data is somewhat right
        if(date.length() == 10 && period > 0) {
        
            // Substrings the year, month and day from the string
            int year = Integer.parseInt(date.substring(6, 10));
            int month = Integer.parseInt(date.substring(3, 5)) - 1;
            int day = Integer.parseInt(date.substring(0, 2));
            
            // Makeing a new Gregorian Calendar from the dates and make it a date-type
            Date d1 = new GregorianCalendar(year, month, day).getTime();
            // Getting the timestamp for right now
            Date today = new Date();
            
            // Taking the UNIX-TIME in miliseconds and substract it
            // Also making the miliseconds to days
            long diff = (today.getTime() - d1.getTime()) / (1000 * 60 * 60 * 24);
            
            // Checking if the difference between the allowed period and the diffrence is bigger than 0
            if(period-diff >= 0) {
                // Returning false, it wasn't too late
                return false;
            } else {
                // Returning true, it is too late
                return true;
            }
        } else {
            // Just tells that it was too late (fuck the customers)!!
            return true;
        }
    }   
}